package xyz.xtt.user.controller;

import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.objects.DozerUtil;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.user.api.dto.TblUserFundRecordDto;
import xyz.xtt.user.client.request.UserRegRequest;
import xyz.xtt.user.client.response.UserInfoResponse;
import xyz.xtt.user.client.response.UserRegResponse;
import xyz.xtt.user.entity.TblUser;
import xyz.xtt.user.entity.TblUserFundRecord;
import xyz.xtt.user.service.ITblUserChainAccountService;
import xyz.xtt.user.service.ITblUserFundAccountService;
import xyz.xtt.user.service.ITblUserFundRecordService;
import xyz.xtt.user.service.ITblUserService;
import xyz.xtt.user.vo.user.UserVo;

/**
 * @author huqibo
 *
 */
@Tag(name = "用户资金账户管理", description = "用户资金账户管理")
@Slf4j
@RequestMapping("/api/nft/user")
@RestController
@Controller
public class InnerUserController {
	@Autowired
	private ITblUserService iTblUserService;
	@Autowired
	private ITblUserChainAccountService iTblUserChainAccountService;
	@Autowired
	private ITblUserFundRecordService iTblUserFundRecordService;
	@Autowired
	private ITblUserFundAccountService iTblUserFundAccountService;

	/**
	 * 查询用户信息
	 */
	@GetMapping("/info")
	public ApiResult<UserInfoResponse> info(Long appUid) {
		return ApiResult.ok(UserInfoResponse.builder().build());
	}

	/**
	 * 批量查询用户信息
	 */
	@GetMapping("/batchInfo")
	public ApiResult<List<UserInfoResponse>> batchInfo(Collection<Long> appUid) {
		UserInfoResponse build = UserInfoResponse.builder().build();
		return ApiResult.ok(Lists.newArrayList(build));
	}

	@GetMapping("/getUserInfo")
	public ApiResult<?> getUserInfo(@RequestHeader("uid") String uid, @RequestParam(value = "signUserId", defaultValue = "") String userId) {
		return ApiResult.ok(iTblUserService.getUserInfo(StringUtils.hasText(userId) ? userId : uid));
	}

	/**
	 * 获取用户信息
	 */
	@Operation(summary = "获取用户信息", description = "uid必须存在")
	@GetMapping("/getAccount")
	public ApiResult<UserVo> getAccountAddress(@RequestParam("uid") String uid) {
		log.info("/api/nft/user/getAccount uid:{}", uid);
		UserVo accountAddress = iTblUserChainAccountService.getAccountAddress(uid);
		return ApiResult.ok(accountAddress);
	}

	@PostMapping("/register")
	public ApiResult<UserRegResponse> register(@RequestBody UserRegRequest userRegRequest) {
		log.info("/api/nft/user/userReg param:{}", JSONUtil.toJsonStr(userRegRequest));
		UserRegResponse response = iTblUserService.register(userRegRequest);
		return ApiResult.ok(response);
	}

	@Operation(summary = "是否需要实名认证", description = "是否需要实名认证")
	@GetMapping("/checkAuth")
	public ApiResult<Boolean> checkAuth(String uid, Long authCheckFrequency) {
		log.info("/api/nft/user/checkAuth uid:{},authCheckFrequency:{}", uid, authCheckFrequency);
		boolean checkAuth = iTblUserService.checkAuth(uid, authCheckFrequency);
		return ApiResult.ok(checkAuth);
	}

//	@GetMapping("/getNeedSettleRecords")
//	public ApiResult<List<TblUserFundRecordDto>> getNeedSettleRecords(@RequestParam("maxCount") @Positive Integer maxCount,
//			@RequestParam(value = "lastId", required = false) Long lastId) {
//		List<TblUserFundRecord> needSettleRecords = iTblUserFundRecordService.getNeedSettleRecords(maxCount, lastId);
//		return ApiResult.ok(convertFundRecordDtoList(needSettleRecords));
//	}
//
//	@PostMapping("/updateFundSettled")
//	public ApiResult<Boolean> updateFundSettled(@RequestBody @Valid UpdateFundSettledRequest request) {
//		return ApiResult.ok(iTblUserFundAccountService.updateFundSettled(request.getUserFundRecordId()));
//	}

	@GetMapping("/infoWithPhone")
	public ApiResult<UserInfoResponse> infoWithPhone(String phone) {
		log.info("/api/nft/user/infoWithPhone phone:{}", phone);
		TblUser user = iTblUserService.infoWithPhone(phone);
		UserInfoResponse transfor = DozerUtil.transfor(user, UserInfoResponse.class);
		return ApiResult.ok(transfor);
	}

	private List<TblUserFundRecordDto> convertFundRecordDtoList(List<TblUserFundRecord> needSettleRecords) {
		if (CollectionUtils.isEmpty(needSettleRecords)) {
			return null;
		}
		return needSettleRecords.stream().map(record -> {
			TblUserFundRecordDto dto = new TblUserFundRecordDto();
			BeanUtils.copyProperties(record, dto);
			return dto;
		}).toList();
	}

}